﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GmailMetroNotifier.Client.Properties;

namespace GmailMetroNotifier.Client.Common
{
    /// <summary>
    /// Provides logging functionality to Windows event log.
    /// </summary>
    public static class Logger
    {
        /// <summary>
        /// Logs an informational message.
        /// </summary>
        /// <param name="message">The message to be logged.</param>
        /// <param name="args">The values to replace the format items in the message.</param>
        public static void LogInformation(string message, params object[] args)
        {
            LogMessage(MessageSeverity.Information, message, args);
        }

        /// <summary>
        /// Logs a warning message.
        /// </summary>
        /// <param name="message">The message to be logged.</param>
        /// <param name="args">The values to replace the format items in the message.</param>
        public static void LogWarning(string message, params object[] args)
        {
            LogMessage(MessageSeverity.Warning, message, args);
        }

        /// <summary>
        /// Logs an error message.
        /// </summary>
        /// <param name="message">The message to be logged.</param>
        /// <param name="args">The values to replace the format items in the message.</param>
        public static void LogError(string message, params object[] args)
        {
            LogMessage(MessageSeverity.Error, message, args);
        }

        private static void LogMessage(MessageSeverity severity, string message, params object[] args)
        {
            if (Settings.Default.IsLoggingEnabled)
            {
                if (!EventLog.SourceExists(Constants.ApplicationName))
                {
                    EventLog.CreateEventSource(Constants.ApplicationName, Constants.DefaultLogSourceName);
                }

                string validMessage = (args != null && args.Length > 0)
                    ? string.Format(CultureInfo.CurrentCulture, message, args)
                    : message;

                EventLogEntryType entryType = ConvertSeverityToEntryType(severity);

                EventLog.WriteEntry(Constants.ApplicationName, validMessage, entryType);
            }
        }

        private static EventLogEntryType ConvertSeverityToEntryType(MessageSeverity severity)
        {
            EventLogEntryType entryType = EventLogEntryType.Information;

            switch (severity)
            {
                case MessageSeverity.Information:
                    entryType = EventLogEntryType.Information;
                    break;

                case MessageSeverity.Warning:
                    entryType = EventLogEntryType.Warning;
                    break;

                case MessageSeverity.Error:
                case MessageSeverity.Critical:
                    entryType = EventLogEntryType.Error;
                    break;
            }

            return entryType;
        }
    }
}
